package com.itzyf.pokemondata.activity.panel

import android.app.Activity
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.DbDivPokemon
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_add_pokemon.*
import org.litepal.LitePal
import java.util.*

class EditPokemonActivity : BaseActivity() {
    private var pokemon: DbDivPokemon? = null
    override fun handlerActivity() {
        val id = intent.getLongExtra("id", 0L)
        if (id > 0) {
            pokemon = LitePal.find(DbDivPokemon::class.java, id)
            pokemon?.let {
                etHp.setText(it.hp.toString())
                etPa.setText(it.pa.toString())
                etPd.setText(it.pd.toString())
                etSa.setText(it.sa.toString())
                etSd.setText(it.sd.toString())
                etSpeed.setText(it.speed.toString())
                etSt.setText(it.specialTraining)
                etName.setText(it.pokemonName)
                setTitle(it.pokemonName)
            }

        }
        btnSubmit.setOnClickListener {
            save()
        }
    }

    override fun getContentViewId() = R.layout.activity_add_pokemon

    override fun getToolbarTitle() = "添加自定义精灵数据"

    private fun save() {
        val hp = etHp.text.toString()
        val pa = etPa.text.toString()
        val pd = etPd.text.toString()
        val sa = etSa.text.toString()
        val sd = etSd.text.toString()
        val speed = etSpeed.text.toString()

        val pokemonName = etName.text.toString()
        val st = etSt.text.toString()

        if (pokemonName.isEmpty()) {
            showToast("请输入名称")
            return
        }
        if (hp.isEmpty()) {
            showToast("请输入血量")
            return
        }
        if (pa.isEmpty()) {
            showToast("请输入物攻")
            return
        }
        if (pd.isEmpty()) {
            showToast("请输入物防")
            return
        }
        if (sa.isEmpty()) {
            showToast("请输入特攻")
            return
        }
        if (sd.isEmpty()) {
            showToast("请输入特防")
            return
        }
        if (speed.isEmpty()) {
            showToast("请输入速度值")
            return
        }
        if (st.isEmpty()) {
            showToast("请输入特训")
            return
        }

        val sts = st.split(",")
        if (sts.size != 6) {
            showToast("请正确输入特训")
            return
        }
        val db = DbDivPokemon()
        db.pokemonName = pokemonName
        db.hp = Integer.parseInt(hp)
        db.pa = Integer.parseInt(pa)
        db.pd = Integer.parseInt(pd)
        db.sa = Integer.parseInt(sa)
        db.sd = Integer.parseInt(sd)
        db.speed = Integer.parseInt(speed)
        db.specialTraining = st
        db.red1 = ""
        db.createdAt = Date()
        if (pokemon != null && pokemon!!.id > 0) {
            db.update(pokemon!!.id)
        } else {
            db.save()
        }

        showToast("保存成功")
        setResult(Activity.RESULT_OK)
        finish()
    }

}